package com.preskool.serevice.impl;

import com.preskool.mapper.LibraryMapper;
import com.preskool.po.Library;
import com.preskool.po.LibraryExample;
import com.preskool.dto.LibraryVo;
import com.preskool.serevice.LibraryService;
import org.apache.tomcat.util.net.jsse.JSSEUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.script.ScriptEngine;
import java.util.List;

/**
 * @Author:薛亮亮
 * @Date 2021/1/8 14:31
 * @Discription:图书馆业务实现层
 */
@Service
public class LibraryServiceImpl implements LibraryService {
    @Resource
    private LibraryMapper libraryMapper;
/*    @Override
    public List<Library> findLibrarys(Integer offset,Integer limit) {
        LibraryExample example = new LibraryExample();
        //设置页码,每页显示条数
        offset=(offset-1)*limit;
        example.setOffset(offset);
        example.setLimit(limit);
        List<Library> libraries = libraryMapper.selectByExample(example);

        return libraries;
    }*/

    /**
     * 添加图书信息
     *
     * @param library
     * @return
     */
    @Override
    public int addLibrary(Library library) {
        //创建查询对象
        LibraryExample example = new LibraryExample();
        LibraryExample.Criteria criteria = example.createCriteria();
        //查询部门和班级id
        LibraryExample.Criteria criteria1 = criteria.andDeptNameEqualTo(library.getDeptName());

        LibraryExample.Criteria criteria2 = criteria.andClidEqualTo(library.getClid());
//        System.out.println(criteria1.toString());
//        System.out.println(criteria2.toString());
        //判断班级id和部门名称是否存在,存在则添加,不存在则不添加
        List<Library> libraries = libraryMapper.selectByExample(example);


        int i = 0;
        try {
            i = libraryMapper.insertSelective(library);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }


        return i;
    }

    /**
     * 删除图书数据
     *
     * @param lid 图书信息的lid
     * @return
     */
    @Override
    public int deleteLibrary(Integer lid) {
        int i = 0;
        try {
            i = libraryMapper.deleteLibrary(lid);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
        return i;
    }

    /**
     * 更新图书信息
     *
     * @param library
     * @return
     */
    @Override
    public int updateLibrary(Library library) {

        int i = 0;
        try {
            i = libraryMapper.updateByPrimaryKeySelective(library);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
        return i;
    }


    /**
     * 查询所有图书馆图的书信息
     *
     * @return
     */
    @Override
    public List<LibraryVo> findAllLibrary(Integer offset, Integer limit, String sortName) {
        LibraryExample example = new LibraryExample();
        //设置页码,每页显示条数以及查询条件
        offset = (offset - 1) * limit;
        example.setOffset(offset);
        example.setLimit(limit);
        example.setOrderByClause(sortName);
        List<LibraryVo> libraries = libraryMapper.findAllLibrary(example);
        return libraries;
    }
}
